package com.yuexun.dhdoctor.module.Contract.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.module.Contract.adapter.DeleteMemberAdapter;
import com.yuexun.dhdoctor.module.Contract.bean.People;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/7/25.
 */
public class Act_DeleteMember extends BaseKJActivity {

    @Bind(R.id.deletemember_list)
    SwipeMenuListView deletememberList;

    ArrayList<People> usersBeans_origin = new ArrayList<People>();
    DeleteMemberAdapter adapter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_deletemember);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        usersBeans_origin = (ArrayList<People>) getIntent().getExtras().getSerializable("persons");
        adapter = new DeleteMemberAdapter(deletememberList, usersBeans_origin, R.layout.item_deletemember);
        deletememberList.setAdapter(adapter);
//        set_swipelist();
    }

    private void set_swipelist() {
        // step 1. create a MenuCreator
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set a icon
                deleteItem.setIcon(R.drawable.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        deletememberList.setMenuCreator(creator);

        // step 2. listener item click event
        deletememberList.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // delete
//                        delete(position);
                        break;
                }
                return false;
            }
        });

        // set SwipeListener
        deletememberList.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

            @Override
            public void onSwipeStart(int position) {
                // swipe start
            }

            @Override
            public void onSwipeEnd(int position) {
                // swipe end
            }
        });
    }

    private void delete(ArrayList<People> peoples) {
        for (int i = 0; i < peoples.size(); i++) {
            for (int j = 0; j < usersBeans_origin.size(); j++) {
                if (usersBeans_origin.get(j).getUserId().equals(peoples.get(i).getUserId()))
                    usersBeans_origin.remove(j);
            }
        }
        adapter = new DeleteMemberAdapter(deletememberList, usersBeans_origin, R.layout.item_deletemember);
        deletememberList.setAdapter(adapter);
    }

    @OnClick({R.id.back, R.id.myshare_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                bundle.putSerializable("persons", usersBeans_origin);
                intent.putExtras(bundle);
                setResult(101, intent);
                finish();
                break;
            case R.id.myshare_delete:
                delete(adapter.getArrayList());
                break;
        }
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}
